package spotGUI.personalProfileGUI;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import personalProfile.PhotoManager;
import personalProfile.PhotoManager.PhotoAlbum;

public class PhotoPanel extends JPanel implements Observer {

	private JTabbedPane tabbedPane;
	private PhotoManager manager;
	private AddPhotosPanel addPhotoPanel;

	public PhotoPanel(PhotoManager manager) {
		setOpaque(false);

		this.manager = manager;
		manager.addObserver(this);

		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{222, 0};
		gridBagLayout.rowHeights = new int[]{206, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);


		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setOpaque(true);
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

		GridBagConstraints gbc_tabbedPane = new GridBagConstraints();
		gbc_tabbedPane.fill = GridBagConstraints.BOTH;
		gbc_tabbedPane.insets = new Insets(0, 0, 5, 0);
		gbc_tabbedPane.gridx = 0;
		gbc_tabbedPane.gridy = 0;
		add(tabbedPane, gbc_tabbedPane);

		addPhotoPanel = new AddPhotosPanel(manager);
		addPhotoPanel.setVisible(false);
		GridBagConstraints gbc_addPhotoPanel = new GridBagConstraints();
		gbc_addPhotoPanel.fill = GridBagConstraints.HORIZONTAL;
		gbc_addPhotoPanel.anchor = GridBagConstraints.SOUTH;
		gbc_addPhotoPanel.gridx = 0;
		gbc_addPhotoPanel.gridy = 1;
		add(addPhotoPanel, gbc_addPhotoPanel);

		tabbedPane.addChangeListener(new ChangeListener() {

			public void stateChanged(ChangeEvent evt) {
				JTabbedPane pane = (JTabbedPane)evt.getSource();
				
				if (pane.getSelectedIndex() != -1) {
					TabComponent tab = (TabComponent) pane.getTabComponentAt(pane.getSelectedIndex());
					if (tab != null)
						addPhotoPanel.setSelectedAlbum(tab.getAlbum());
				}
			}
		});

	}


	private void addAlbum (PhotoAlbum album) {
		
		AlbumView aw = new AlbumView(album, manager);
		JScrollPane scrollPane = new JScrollPane(aw);
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.addComponentListener(aw);
		tabbedPane.insertTab(null, null, scrollPane, null, 0);
		tabbedPane.setTabComponentAt(0, new TabComponent(album));
		tabbedPane.setSelectedIndex(0);
		addPhotoPanel.setSelectedAlbum(album);
	}

	public void makeEditable () {
		addPhotoPanel.setVisible(true);
		for (int i = 0; i < tabbedPane.getTabCount(); i++) {
			((AlbumView) ((JScrollPane)tabbedPane.getComponentAt(i)).getViewport().getView()).enablePhotoRemoval();
		}
	}


	@Override
	public void update(Observable o, Object arg) {
		if (tabbedPane.getTabCount() > 0 && ((TabComponent)tabbedPane.getTabComponentAt(0)).getAlbum().albumid == PhotoManager.NO_ALBUMS)
			tabbedPane.removeTabAt(0);
		
		if (arg instanceof PhotoAlbum) {
			addAlbum ((PhotoAlbum)arg);
		}
	}


	public class TabComponent extends JPanel {

		private JLabel lblCover;
		private JTextArea textArea;
		private PhotoAlbum album;

		public TabComponent(PhotoAlbum album) {

			this.album = album;

			setOpaque(false);

			GridBagLayout gridBagLayout = new GridBagLayout();
			gridBagLayout.columnWidths = new int[]{12, 0};
			gridBagLayout.rowHeights = new int[]{35, 0, 0};
			gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
			gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
			setLayout(gridBagLayout);

			lblCover = new JLabel("");
			if (album.icon != null)
				lblCover.setIcon(album.icon);
			else
				lblCover.setIcon(new ImageIcon (PhotoPanel.class.getResource("/images/others/album.png")));
			lblCover.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
			GridBagConstraints gbc_lblCover = new GridBagConstraints();
			gbc_lblCover.insets = new Insets(0, 0, 5, 0);
			gbc_lblCover.anchor = GridBagConstraints.NORTHWEST;
			gbc_lblCover.gridx = 0;
			gbc_lblCover.gridy = 0;
			add(lblCover, gbc_lblCover);

			textArea = new JTextArea();
			textArea.setFont(new Font("Agency FB", Font.BOLD, 16));
			textArea.setText(album.name);
			textArea.setEditable(false);
			textArea.setLineWrap(true);
			textArea.setWrapStyleWord(true);
			textArea.setOpaque(false);
			GridBagConstraints gbc_textArea = new GridBagConstraints();
			gbc_textArea.fill = GridBagConstraints.BOTH;
			gbc_textArea.gridx = 0;
			gbc_textArea.gridy = 1;
			add(textArea, gbc_textArea);

		}

		public PhotoAlbum getAlbum () {
			return album;
		}
	}


	public void updateSelectedCover() {
		int i = tabbedPane.getSelectedIndex();
		tabbedPane.setTabComponentAt(i, new TabComponent(((TabComponent)tabbedPane.getTabComponentAt(i)).getAlbum()));
	}

}
